package com.sdyeda.readiot.config;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import com.sdyeda.readiot.iotinfo.IotData;
import com.sdyeda.readiot.iotinfo.ReadingInfo;
import com.sdyeda.readiot.tools.TimeTools;

public class WriteLog {
	public static void WriteReaddingIofo() throws IOException {

		String line = "------------------------------------------------------------------------Begin-------------------------------------------------------------------------------------\n";
		String endl = "-------------------------------------------------------------------------End--------------------------------------------------------------------------------------\n\n\n\n";
		String totle= "\n统计：成功读取(%d/%d)个。读取用时%s（开始读取时间：%s,结束时间：%s）\n";
		String sline= "------------------------------------------------------------------------华丽分割线----------------------------------------------------------------------------------\n\n";
		String goodl= "-----------------------------------------------------------------------成功读取统计---------------------------------------------------------------------------------\n";
		String badl = "-----------------------------------------------------------------------失败读取统计---------------------------------------------------------------------------------\n";
		String head = "|     串口     |   编号   |     设备编号     | 类型编号 |  读取值  |  偏差值  |  温度   | 应变频率 | 补偿频率  |    类型    | 单位 |           时间          |  状态  |读取次数|\n";
		String data = "|%10s |%10s|%16s|%6s  | %8.2f| %8.2f|%8.2f| %8.2f| %8.2f | %10s|%4s |%23s |  %s  |   %d   |\n";
		String timel= "---------------------------------------------------------------------%s-------------------------------------------------------------------------\n";
		
		ReadingInfo readinginfo = (ReadingInfo)Setting.Get(Setting.READINGINFO);
		
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.CHINA);
		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS",Locale.CHINA);
		
		FileOutputStream fos = new FileOutputStream(getFilename(), true);
		OutputStreamWriter  osr = new OutputStreamWriter(fos, "UTF-8");
		osr.write(line);
		osr.write(String.format(timel,sdf1.format(readinginfo.getBegin())));
		//统计全部
		osr.write(String.format(totle, 
				readinginfo.getSumSuccIots(),
				readinginfo.getSumIots(),
				TimeTools.getDatePoor(readinginfo.getEnd(), readinginfo.getBegin()),
				sdf1.format(readinginfo.getBegin()),
				sdf1.format(readinginfo.getEnd())));
		//按串口统计

		osr.write("分串口统计：");
		for (String key : readinginfo.getComSumIots().keySet()) {
			osr.write(key.toString()+":");
			osr.write(String.format("(%d",readinginfo.getComSumSuccIots().get(key)==null?0:readinginfo.getComSumSuccIots().get(key)));
			osr.write(String.format("/%d)   ",readinginfo.getComSumIots().get(key)==null?0:readinginfo.getComSumIots().get(key)));
		}
		osr.write("\n");
		//按类型统计
		
		osr.write("分类型统计：");
		for (String key : readinginfo.getTypeSumIots().keySet()) {
			osr.write("类型"+key.toString()+":");
			osr.write(String.format("(%d",readinginfo.getTypeSumSuccIots().get(key)==null?0:readinginfo.getTypeSumSuccIots().get(key)));
			osr.write(String.format("/%d)  ",readinginfo.getTypeSumIots().get(key)==null?0:readinginfo.getTypeSumIots().get(key)));
		}
		osr.write("\n");
		
		//System.out.println(readinginfo.getComSumIots());
		//System.out.println(readinginfo.getSumSuccIots());
		//osr.write(sline);
		osr.write(goodl);
		osr.write(head);

		for(int i=0;i<readinginfo.getList().size();i++)
		{
			IotData item =readinginfo.getList().get(i);
			if(item.isSuccess()) {
				osr.write( String.format(data, 					 //%s %s %s %s %4.3f %4.3f %4.3f %4.3f %4.3f %s %s %s\
						item.getCom().trim(),
						item.getId().trim(),
						item.getIotid().trim(),
						item.getType().trim(),
						item.getValue(),
						item.getOffset(),
						item.getTemp(),
						item.getCompen(),
						item.getStrain(),
						item.getStype(),
						item.getUnit(),
						sdf2.format(item.getTime().getTime()),
						item.isSuccess()?"成功":"失败",
						item.getReadtimes()));
			}
		}
		
		osr.write(sline);
		osr.write(badl);
		osr.write(head);
		for(int i=0;i<readinginfo.getList().size();i++)
		{
			IotData item =readinginfo.getList().get(i);
			if(!item.isSuccess()) {
				osr.write( String.format(data, 					 //%s %s %s %s %4.3f %4.3f %4.3f %4.3f %4.3f %s %s %s\
						item.getCom().trim(),
						item.getId().trim(),
						item.getIotid().trim(),
						item.getType().trim(),
						item.getValue(),
						item.getOffset(),
						item.getTemp(),
						item.getCompen(),
						item.getStrain(),
						item.getStype(),
						item.getUnit(),
						item.getTime()==null?"":sdf2.format(item.getTime().getTime()),
						item.isSuccess()?"成功":"失败",
						item.getReadtimes()));
				 
			}
		}
		osr.write(endl);


		osr.close();
		fos.close();
					
	}
	

	
	static private String getFilename() {
		
		//按日期生成文件名
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd",Locale.CHINA);
		String dir = (String)Setting.Get(Setting.INFODIR);
		//目录增加 /
		if(dir.indexOf("/") != dir.length()-1) {
			dir=dir+"/";
		}
		return dir+sdf.format(new Date())+".log";
	}
	

	
}
